﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Input;

namespace DcsGauges.Server.ViewModels
{
    abstract class ViewModelBase : INotifyPropertyChanged
    {
        #region Fields

        private string error            = null;
        public string Error
        {
            get { return this.error; }
            set
            {
                if(this.error != value)
                {
                    this.error = value;
                    this.OnPropertyChanged(MethodBase.GetCurrentMethod());
                }
            }
        }

        private bool hasPendingChanges = false;
        public bool HasPendingChanges
        {
            get { return this.hasPendingChanges; }
            set
            {
                if(this.hasPendingChanges != value)
                {
                    this.hasPendingChanges = value;
                    this.OnPropertyChanged(MethodBase.GetCurrentMethod(), changeHasPendingChangesStatus: false);
                }
            }
        }

        #endregion


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler  PropertyChanged;
       
        #endregion


        #region IDisposable Members

        public virtual void Dispose()
        {

        }

        #endregion


        #region Event Handling


        protected void OnPropertyChanged(string property, bool updateHasPendingChangesStatus = true)
        {
            if(this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
            if(updateHasPendingChangesStatus == true)
                this.HasPendingChanges = true;
        }


        protected void OnPropertyChanged(MethodBase methodBase, bool changeHasPendingChangesStatus = true)
        {
            OnPropertyChanged(methodBase.Name.Substring(4), changeHasPendingChangesStatus);
        }

        #endregion


        #region Command Operations

        protected void UpdateCommandStatus(ICommand command)
        {
            dynamic cmd = (dynamic)command;
            cmd.RaiseCanExecuteChanged();
        }

        #endregion
    }
}
